package interpreters;

import java.util.*;

import org.hibernate.Transaction;

import Factory.HibernateSessionFactory;
import dataObjects.*;
import definitions.*;

public class ConflictChecker {
	
	public static void main(String[] args){
		List<Conflict> list = getConflicts("woodsd");
		System.out.println("Printing Conflicts...\n");
		
		for(Conflict c:list){
			System.out.println(c);
		}
		System.out.println("Done Printing Conflicts...\n");
	}
	
	public static LinkedList<Conflict> getConflicts(String userName){
		LinkedList<Conflict> conflicts = new LinkedList<Conflict>();
		
		List<Dose> doses;
		DoseDAO doseDao = new DoseDAO();
		Transaction tx = doseDao.getSession().beginTransaction();
		doses = doseDao.findByUserName(userName);
		tx.commit();
		doseDao.getSession().close();
		
		MedicineData medicine;
		MedicineDataDAO medDao = new MedicineDataDAO();
		HibernateSessionFactory.setConfigFile(HibernateSessionFactory.CONFIG_FILE_LOCATION_MEDICINES);
		Transaction medtx = medDao.getSession().beginTransaction();
		
		MedicineData[] medData = new MedicineData[doses.size()];
		
		for(int i = 0; i < doses.size(); i++){
			String med1 = doses.get(i).getId().getMedicine();
			List fromDao = medDao.findByMedicineName(med1);
			if(fromDao.size() == 0) continue;
			medData[i] = (MedicineData) medDao.findByMedicineName(med1).get(0);
		}

		medtx.commit();
		medDao.getSession().close(); 
		
		FoodingredientsDAO foodDao = new FoodingredientsDAO();
		HibernateSessionFactory.setConfigFile(HibernateSessionFactory.CONFIG_FILE_LOCATION_DIETARY_MONITOR);
		Transaction foodtx = foodDao.getSession().beginTransaction();
		
		for(int i = 0; i < medData.length-1; i++){
			if(medData[i] == null) continue;
			if(medData[i].getConflictingMedicines() == null || medData[i].getConflictingMedicines().equals("")) continue;
			String[] conflictingMeds = medData[i].getConflictingMedicines().split(",");
			for(int j = i+1; j < doses.size(); j++){
				//if(i == j) continue;
				String med2 = doses.get(j).getId().getMedicine();
				//Conflicts are always equal, but should not be.  You call toLowerCase on Med2 twice instead of just using .equalsIgnoreCase
				//
				for(String s:conflictingMeds){
					if(s.equalsIgnoreCase(med2)){
						Conflict c = new Conflict(medData[i].getMedicineName(), med2);
						if(!conflicts.contains(c)) conflicts.add(c);
						break;
					}
				}
			}
			
			List<Foodingredients> conflictingFoods = foodDao.findByMedicineData(medData[i]);
			for(Foodingredients f:conflictingFoods){
				Conflict c = new Conflict(medData[i].getMedicineName(), f.getIngredients().getName());
				if(!conflicts.contains(c)) conflicts.add(c);
			}
		}

		foodtx.commit();
		foodDao.getSession().close();
		
//		conflicts.add(new Conflict("Vicodin", "Decency"));
//		conflicts.add(new Conflict("Arsenic", "living"));
//		conflicts.add(new Conflict("cats", "dogs"));
//		conflicts.add(new Conflict("cats", "other things"));
//		conflicts.add(new Conflict("Quentin Tarantino", "Good Movies"));
//		conflicts.add(new Conflict("Politicians", "Intelligence"));
//		conflicts.add(new Conflict("monkeys", "not throwing shit"));
		
		return conflicts;
	}
}
